#!/bin/bash
#
# Copyright (C) 2017 Julien Desfossez <jdesfossez@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only
#

TEST_DESC="Kernel tracer - lttng-logger"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
NUM_TESTS=30
PAYLOAD="test_logger"
SESSION_NAME="kernel_event_basic"

source $TESTDIR/utils/utils.sh

function test_proc_logger()
{
	diag "Test /proc/lttng-logger"

	local TRACE_PATH=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	lttng_enable_kernel_event $SESSION_NAME "lttng_logger"

	start_lttng_tracing_ok

	test -e /proc/lttng-logger
	if test $? = 0; then
		pass "/proc/lttng-logger exists"
		echo -n "$PAYLOAD proc" > /proc/lttng-logger
		ok $? "Write in /proc/lttng-logger"
	else
		fail "No /proc/lttng-logger"
	fi

	stop_lttng_tracing_ok

	validate_trace_count "lttng_logger" $TRACE_PATH 1
	validate_trace_only_exp "$PAYLOAD" $TRACE_PATH

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH
}

function test_dev_logger()
{
	diag "Test /dev/lttng-logger"

	local TRACE_PATH=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	lttng_enable_kernel_event $SESSION_NAME "lttng_logger"

	start_lttng_tracing_ok

	test -c /dev/lttng-logger
	if test $? = 0; then
		pass "/dev/lttng-logger is a character device"
		echo -n "$PAYLOAD dev" > /dev/lttng-logger
		ok $? "Write in /dev/lttng-logger"
	else
		fail "No /dev/lttng-logger"
	fi

	stop_lttng_tracing_ok

	validate_trace_count "lttng_logger" $TRACE_PATH 1
	validate_trace_only_exp "$PAYLOAD" $TRACE_PATH

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH
}

function test_payload_limit()
{
	diag "Test lttng-logger payload limit"

	local TRACE_PATH=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH

	lttng_enable_kernel_event $SESSION_NAME "lttng_logger"

	start_lttng_tracing_ok

	# Write 100 times "test_logger", which generates 1200 characters, we expect
	# the tracer to write 2 events from that string because it limits the
	# input to 1024 strings.
	printf "%.s $PAYLOAD" {1..100} > /proc/lttng-logger
	printf "%.s $PAYLOAD" {1..100} > /dev/lttng-logger

	stop_lttng_tracing_ok

	validate_trace_count "lttng_logger" $TRACE_PATH 4
	validate_trace_only_exp "$PAYLOAD" $TRACE_PATH

	destroy_lttng_session_ok $SESSION_NAME

	rm -rf $TRACE_PATH
}

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." ||
{
	validate_lttng_modules_present
	start_lttng_sessiond

	test_proc_logger
	test_dev_logger
	test_payload_limit

	stop_lttng_sessiond
}
